<template>
  <div id="taskSTopSearch">
    <h1 class="con-right-title">
      首页推荐管理
    </h1>
    <div class="search-box">
      <el-form
        ref="ruleForm"
        :inline="true"
        :model="formInline"
        class="demo-form-inline"
      >
        <el-form-item label="置顶时间" prop="createTime">
          <el-date-picker
            v-model="formInline.createTime"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            value-format="yyyy-MM-dd HH:mm:ss"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="置顶类别" prop="topType">
          <el-select v-model="formInline.topType" clearable placeholder="">
            <el-option
              v-for="item in topTypeList"
              :key="item.dictLabel"
              :label="item.dictLabel"
              :value="item.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="内容类型" prop="contentType">
          <el-select
            clearable
            v-model="formInline.contentType"
            placeholder=""
            :disabled="cantChoose"
          >
            <el-option
              v-for="item in contentTypeList"
              :key="item.dictLabel"
              :label="item.dictLabel"
              :value="item.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户属性" prop="userProperty">
          <el-select v-model="formInline.userProperty" clearable>
            <el-option
              v-for="item in userAttList"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item class="s-btn">
          <el-button
            type="success"
            icon="el-icon-search"
            @click="toSearch('ruleForm')"
          >
            查询
          </el-button>
          <el-button type="success" plain @click="reset">
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { sysNormalDisable } from '@utils/dictionariesFunc'
export default {
  name: 'TaskSTopSearch',
  msg: '作业作品生活置顶排序-顶部搜索',
  props: {
    formInline: {
      type: Object,
      default: function() {
        return {
          topType: '', // 置顶类别
          contentType: '', // 内容类型
          userProperty: '', // 用户属性
          startTime: '', // 开始时间
          endTime: '', // 结束时间
          createTime: []
        }
      }
    }
  },
  data() {
    return {
      // 置顶类别
      topTypeList: [{ dictLabel: '全部', dictValue: '' }],
      // 内容类型
      contentTypeList: [{ dictLabel: '全部', dictValue: '' }],
      // 查询类型
      checkTypeList: [],
      createTime: [], // 创建时间
      userAttList: [{ dictLabel: '全部', dictValue: '' }], // 用户属性
      workValue: 'WORK',
      videoValue: 'MOVIE',
      cantChoose: false
    }
  },
  watch: {
    'formInline.createTime': {
      handler(newVal, oldVal) {
        if (newVal && newVal.length > 1) {
          this.formInline.startTime = newVal[0]
          this.formInline.endTime = newVal[1]
        } else {
          this.formInline.startTime = ''
          this.formInline.endTime = ''
        }
      },
      deep: true
    },
    'formInline.topType': {
      handler(newVal, oldVal) {
        if (newVal === this.workValue || newVal === this.videoValue) {
          this.cantChoose = true
          this.formInline.contentType = ''
        } else {
          this.cantChoose = false
        }
      },
      deep: true
    }
  },
  mounted() {
    this.getStickList()
  },
  methods: {
    /* 搜索 */
    toSearch(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$emit('toSearch')
        } else {
          return false
        }
      })
    },
    /* 重置 */
    reset() {
      this.$refs['ruleForm'].resetFields()
      //设置默认数据
      this.setDefaultValue()
    },
    /* 获取数据状态 */
    async getStickList() {
      // 置顶类别
      let data = await sysNormalDisable('sys_jobwork_top', 'topType')
      this.topTypeList = this.topTypeList.concat(data)
      // 查询类型
      this.checkTypeList = await sysNormalDisable('task_works_tops_query_type')
      this.setDefaultValue()
      //触发列表获取权限
      this.$emit('toSearch')
      // 内容类型
      let data2 = await sysNormalDisable('sys_task_type', 'contentType')
      this.contentTypeList = this.contentTypeList.concat(data2)
      // 用户属性
      let UserAttr = await sysNormalDisable(
        'top_user_attributes',
        'topUserAttr'
      )
      this.userAttList = this.userAttList.concat(UserAttr)
    },
    /*设置默认数据*/
    setDefaultValue() {
      this.formInline.keyType = this.checkTypeList[0].dictValue
    }
  }
}
</script>

<style lang="less">
// #logTopSearch {
// }
</style>
